#include "gpshelper.h"
#include "math.h"


static double pi = 3.14159265358979324;
static double a = 6378245.0;
static double ee = 0.00669342162296594323;
static double x_pi = 3.14159265358979324 * 3000.0 / 180.0;

GpsHelper::GpsHelper()
{

}

double GpsHelper::transformLat(double lat, double lon)
{
    double ret = 0.0;
    ret = -100.0 + 2.0 * lat + 3.0 * lon + 0.2 * lon * lon + 0.1 * lat * lon +0.2 * sqrt(fabs(lat));
    ret += (20.0 * sin(6.0 * lat * pi) + 20.0 * sin(2.0 * lat * pi)) * 2.0 / 3.0;
    ret += (20.0 * sin(lon * pi) + 40.0 * sin(lon / 3.0 * pi)) * 2.0 / 3.0;
    ret += (160.0 * sin(lon / 12.0 * pi) + 320 * sin(lon * pi  / 30.0)) * 2.0 / 3.0;
    return ret;
}

double GpsHelper::transformLon(double lat, double lon)
{
    double ret = 0.0;
    ret = 300.0 + lat + 2.0 * lon + 0.1 * lat * lat + 0.1 * lat * lon + 0.1 * sqrt(fabs(lat));
    ret += (20.0 * sin(6.0 * lat * pi) + 20.0 * sin(2.0 * lat * pi)) * 2.0 / 3.0;
    ret += (20.0 * sin(lat * pi) + 40.0 * sin(lat / 3.0 * pi)) * 2.0 / 3.0;
    ret += (150.0 * sin(lat / 12.0 * pi) + 300.0 * sin(lat / 30.0 * pi)) * 2.0 / 3.0;
    return ret;
}

double GpsHelper::wgs2gcj_lati(double lat, double lon)
{
    double dLat = transformLat(lon - 105.0, lat - 35.0);
    double dLon = transformLon(lon - 105.0, lat - 35.0);
    double radLat = lat / 180.0 * pi;
    double magic = sin(radLat);
    magic = 1 - ee * magic * magic;
    double sqrtMagic = sqrt(magic);
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
    dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
    double mgLat = lat + dLat;

    return mgLat;

}

double GpsHelper::wgs2gcj_longi(double lat, double lon)
{
    double dLat = transformLat(lon - 105.0, lat - 35.0);
    double dLon = transformLon(lon - 105.0, lat - 35.0);
    double radLat = lat / 180.0 * pi;
    double magic = sin(radLat);
    magic = 1 - ee * magic * magic;
    double sqrtMagic = sqrt(magic);
    dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
    dLon = (dLon * 180.0) / (a / sqrtMagic * cos(radLat) * pi);
    double mgLon = lon + dLon;

    return mgLon;
}

double GpsHelper::bd2gcj_lati(double lat, double lon)
{
    double x = lon - 0.0065, y = lat - 0.006;
    double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
    double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
    double gg_lat = z * sin(theta);
    return gg_lat;
}

double GpsHelper::bd2gcj_longi(double lat, double lon)
{
    double x = lon - 0.0065, y = lat - 0.006;
    double z = sqrt(x * x + y * y) - 0.00002 * sin(y * x_pi);
    double theta = atan2(y, x) - 0.000003 * cos(x * x_pi);
    double gg_lon = z * cos(theta);
    return gg_lon;
}

double GpsHelper::gcj2bd_lati(double lat, double lon)
{
    double x = lon, y = lat;
    double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
    double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
    double bd_lat = z * sin(theta) + 0.006;
    return bd_lat;

}

double GpsHelper::gcj2bd_longi(double lat, double lon)
{
    double x = lon, y = lat;
    double z = sqrt(x * x + y * y) + 0.00002 * sin(y * x_pi);
    double theta = atan2(y, x) + 0.000003 * cos(x * x_pi);
    double bd_lon = z * cos(theta) + 0.0065;
    return bd_lon;
}

double GpsHelper::wgs2bd_lati(double lat, double lon)
{
    double wgs2gcj_lat = wgs2gcj_lati(lat, lon);
    double wgs2gcj_long = wgs2gcj_longi(lat, lon);
    double gcj2bd_lat = gcj2bd_lati(wgs2gcj_lat, wgs2gcj_long);

    return gcj2bd_lat;
}

double GpsHelper::wgs2bd_longi(double lat, double lon)
{
    double wgs2gcj_lat = wgs2gcj_lati(lat, lon);
    double wgs2gcj_long = wgs2gcj_longi(lat, lon);
    double gcj2bd_lon = gcj2bd_longi(wgs2gcj_lat, wgs2gcj_long);

    return gcj2bd_lon;
}
